package team.first.movieapiv2.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.movieapiv2.domain.Staff;
import team.first.movieapiv2.dto.RequestStaff;
import team.first.movieapiv2.util.MongoDBUtil;
import team.first.movieapiv2.vo.ApiResponse;
import team.first.movieapiv2.vo.ApiResponsePaging;

import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapibootv2.controller
 * @Date: 2021/11/23 23:17
 * @Description：
 */
@RestController
public class StaffController {
    DBCollection collection = MongoDBUtil.getCollection("staff");

    /**
     * 2.4 获取指定编号的演员信息
     * 接口地址：GET /api/v2/staff/{staffId}
     */
    @ApiOperation("1.4 GET: 获取指定编号的影人信息")
    @GetMapping("/api/v2/staff/{staffId}")
    public ApiResponse<Staff> getStaffById(@PathVariable("staffId") Integer staffId) {
        ApiResponse<Staff> apiResponse = new ApiResponse<>();
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("id", staffId);

        // 条件查询
        DBCursor cursor = collection.find(filter_dbobject).sort(new BasicDBObject("id", 1));

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor.toArray();
        // 属性考呗
        List<Staff> staffList = BeanUtil.copyToList(dbObjectList, Staff.class);

        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(staffList.size() > 0 ? staffList.get(0) : null);
        return apiResponse;
    }

    /**
     * 2.5根据条件分页获取的影人信息
     * 接口地址:POST /api/v2/staffs
     */
    @PostMapping("/api/v2/staffs")
    @ApiOperation("1.5 POST： 根据条件分页获取的影人信息")
    public ApiResponsePaging<List<Staff>> getStuff(RequestStaff requestStaff) {
        ApiResponsePaging<List<Staff>> apiResponsePaging = new ApiResponsePaging<>();

        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        if (requestStaff.getChineseName() != null) {
            filter_dbobject.put("chineseName", Pattern.compile("^.*" + requestStaff.getChineseName() + ".*$", Pattern.CASE_INSENSITIVE));
        }
        if (requestStaff.getEnglishName() != null) {
            filter_dbobject.put("englishName", Pattern.compile("^.*" + requestStaff.getEnglishName() + ".*$", Pattern.CASE_INSENSITIVE));
        }

        if (requestStaff.getGender() != null) {
            filter_dbobject.put("gender", requestStaff.getGender());
        }

        if (requestStaff.getRegion() != null) {
            filter_dbobject.put("region", Pattern.compile("^.*" + requestStaff.getRegion() + ".*$", Pattern.CASE_INSENSITIVE));
        }
        //下面执行查询，设置limit,只要10条数据,排序(类mysql orderby) 再建一个BasicDBObject即可，－1表示倒序
        DBCursor cursor = collection.find(filter_dbobject);
        int totalRow = cursor.count();
        if (requestStaff.getPagerSize() != null) {
            cursor.limit(requestStaff.getPagerSize());
        }
        if (requestStaff.getPageNumber() != null) {
            cursor.skip((requestStaff.getPageNumber() - 1) * requestStaff.getPagerSize());
        }
        if (requestStaff.getOrderField() != null) {
            // 根据排序规则排序，默认升序
            if (requestStaff.getOrderDirection() != null) {
                cursor.sort(new BasicDBObject(requestStaff.getOrderField(), "ASC".equalsIgnoreCase(requestStaff.getOrderDirection()) ? 1 : -1));
            } else {
                cursor.sort(new BasicDBObject(requestStaff.getOrderField(), 1));
            }
        }
        List<DBObject> dbObjects = cursor.toArray();
        List<Staff> staffList = BeanUtil.copyToList(dbObjects, Staff.class);

        apiResponsePaging.setCode(200).setSuccess(true).setMessage("请求成功").setData(staffList).setTotalRow(totalRow);

        if (requestStaff.getPagerSize() != null) {
            apiResponsePaging.setPageSize(requestStaff.getPagerSize())
                    .setTotalPage(Convert.toInt(Math.ceil((double) totalRow / requestStaff.getPagerSize())));
        }
        if (requestStaff.getPageNumber() != null) {
            apiResponsePaging.setPageNumber(requestStaff.getPageNumber());
        }

        return apiResponsePaging;
    }
}
